<?php

(defined('BASEPATH')) OR exit('No direct script access allowed');

class Ticket_Model extends CI_Model {
    var $language = 1;//french
    public function __construct() {
        parent::__construct();
        if($this->session->userdata('lang')=='en') $this->language = 2;
    }

    private function _set_where($param = array()) {

        if (isset($param['id']))
            $this->db->where('ticket.id', $param['id']);

        if (isset($param['ticket_name']))
            $this->db->where('ticket_name', $param['ticket_name']);

        if (isset($param['description']))
            $this->db->where('description', $param['description']);

        if (isset($param['expiry_date']))
            $this->db->where('expiry_date', $param['expiry_date']);
        if (isset($param['expiry_date >=']))
            $this->db->where('expiry_date >=', $param['expiry_date >=']);

        if (isset($param['price']))
            $this->db->where('price', $param['price']);

        if (isset($param['img_id']))
            $this->db->where('img_id', $param['img_id']);

        if (isset($param['quantity']))
            $this->db->where('quantity', $param['quantity']);
        
        if (isset($param['quantity >']))
            $this->db->where('quantity >', $param['quantity >']);

        if (isset($param['cat_id'])){
            if(is_array($param['cat_id'])) $this->db->where_in('ticket.cat_id', $param['cat_id']);
            else $this->db->where('cat_id', $param['cat_id']);
        }
        
        $this->db->where('ticket_lang.id_lang',$this->language);
            

        if (isset($param['search']))
            $this->db->like('ticket_name', $param['search']);
    }

    function get_ticket($param = array()) {
        $this->db->select('*');
        $this->db->from('ticket');
        $this->db->join('ticket_lang', 'ticket.id=ticket_lang.id_ticket', 'inner');
        $this->_set_where($param);

        if (isset($param['order_by']))
            $this->db->order_by($param['order_by']);
        else
            $this->db->order_by('ticket_name');

        if (isset($param['limit']) && isset($param['offset']))
            $this->db->limit($param['limit'], $param['offset']);

        return $this->db->get()->result();//$this->db->get('ticket')->result();
    }

    function get_ticket_count($param = array()) {
        return count($this->get_ticket($param));
    }
    
    function get_ticket_array($param = array()) {
        
        $tickets = $this->get_ticket($param);
        $output = array();
        foreach($tickets as $item)
        {
            $output[$item->id] = $item->ticket_name;
            
            if (isset($param['GET_PRICE']))
                $output[$item->id] = $item->price;
//            if (isset($param['GET_PRICE']))
//                $output[$item->id] = $item->price;
        }
        return $output;
    }
    
    /*function create_ticket($param = array()) {        
        if(empty($param['id_lang'])) $param['ticket_lang.id_lang'] = $this->language;
        $this->db->insert('ticket', $param);
        return $this->db->insert_id();
    }*/

    function delete_ticket($data = array()) {
        
        if (isset($data['ticket_id'])){
            $this->db->start_cache();
            $this->db->where('id_ticket', $data['ticket_id']);
            $this->db->delete('ticket_lang');
            
            $this->db->stop_cache();
            $this->db->flush_cache();
            $this->db->where('ticket.id', $data['ticket_id']);
            return $this->db->delete('ticket');
        }
            
    }

    /*function update_ticket($data = array())
    {
        if (isset($data['id']))
        {
            $this->db->where('ticket.id', $data['id']);
            $this->db->update('ticket', $data);
        }
    }*/
    
    function get_all_tags($limit = 0, $offset=10){
        $this->db->select('tag,id');
        $this->db->from('ticket');
        $this->db->join('ticket_lang', 'ticket.id=ticket_lang.id_ticket', 'inner');
        $this->db->where('id_lang',$this->language);
        $this->db->group_by("tag");
        $this->db->order_by("tag", "desc"); 
        $this->db->limit($offset,$limit);
        
        return $this->db->get()->result();
    }
    
    function get_top10ticket_bestseller(){
        $this->db->select('ots_ticket.*,ots_ticket_lang.*,img_name, count(ots_ticket_serial.serial_id) as ct_serial');
        $this->db->from('ticket');
        $this->db->join('ticket_lang', 'ticket.id=ticket_lang.id_ticket', 'inner');
        $this->db->join('ticket_serial', 'ticket.id=ticket_serial.ticket_id', 'inner');
        $this->db->join('image', 'image.id=ticket.img_id', 'left');        
        $this->db->where('img_name is not null',NULL,false);
        $this->db->where('ots_ticket_lang.id_lang',  $this->language);
        $this->db->group_by('ticket_serial.ticket_id');
        $this->db->order_by('ct_serial', 'desc');
        $this->db->limit(10, 0);  
        $results = $this->db->get()->result();
        
        if(empty($results)){
            $this->db->select('ots_ticket.*,ots_ticket_lang.*,img_name');
            $this->db->from('ticket');
            $this->db->join('ticket_lang', 'ticket.id=ticket_lang.id_ticket', 'inner');
            $this->db->join('image', 'image.id=ticket.img_id', 'left');
            $this->db->where('ots_ticket.quantity >0',null, false);
            $this->db->where('expiry_date >="'.date('Y-m-d H:i:s').'"',null, false);
            $this->db->where('ots_ticket_lang.id_lang',  $this->language);
            $this->db->order_by('ots_ticket.id', 'desc');
            $this->db->limit(10, 0);  
            $results = $this->db->get()->result();
        }
        return $results;
    }
    
    function search_ticket($keyword,$limit='',$offset=''){
        
        $this->db->select('ots_ticket.*,ots_ticket_lang.*,img_name');
        $this->db->from('ticket');
        $this->db->join('ticket_lang', 'ticket.id=ticket_lang.id_ticket', 'inner');
        $this->db->join('image', 'image.id=ticket.img_id', 'left');
        $this->db->where('MATCH (ticket_name,description,meta_title) AGAINST ("'.addslashes($keyword).'*" IN BOOLEAN MODE)', NULL, FALSE);
        if(!empty($offset)) $this->db->limit($offset,$limit);
        return $this->db->get()->result();
    }
}
